package com.tengju.bff.interfaces.manage.crm.dashborad.facade;

import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.analysis.crowd.Tag;
import com.tengju.data.domain.model.clickhouse.dashboard.DashBoardMarketRespository;
import com.tengju.data.domain.model.clickhouse.dashboard.DashBoardPeopleAnalysisInfo;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Date: 2021/4/29 14:50
 * @Description: 
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DashboardPeopleAnalysisFacade {

    private final DashBoardMarketRespository dashBoardMarketRespository;

    /**
     * KA人群分析
     * @date 2021/5/7 17:19
     * @return java.util.List<com.tengju.bff.interfaces.shared.KVVO>
     */
    public List<KVVO> getKAPeopleAnalysisInfo() {
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        List<Long> idCodeList = new ArrayList<>();
        DashBoardPeopleAnalysisInfo info = new DashBoardPeopleAnalysisInfo();
        genSecIdCodes.forEach(e -> {
            idCodeList.add(e.getValue());
        });
        if (CollectionUtils.isNotEmpty(idCodeList)) {
            info = dashBoardMarketRespository.getKAPeopleAnalysis(idCodeList);
        }

        List<KVVO> header = List.of(
                KVVO.of("新人低潜力",info.getNewLowerCapacity() == null ? 0 : info.getNewLowerCapacity(), Tag.NEW_USER_LOW_POTENTIAL),
                KVVO.of("新人中潜力",info.getNewMediumCapacity() == null ? 0 : info.getNewMediumCapacity(),Tag.NEW_USER_MID_POTENTIAL),
                KVVO.of("新人高潜力",info.getNewHigherCapacity() == null ? 0 : info.getNewHigherCapacity(),Tag.NEW_USER_HIGH_POTENTIAL),
                KVVO.of("高收益上升",info.getHighEarningsUp() == null ? 0 : info.getHighEarningsUp(),Tag.HIGH_BONUS_RISE),
                KVVO.of("高收益稳定",info.getHighEarningsStab() == null ? 0 : info.getHighEarningsStab(),Tag.HIGH_BONUS_STABLE),
                KVVO.of("高收益下降",info.getHighEarningsDown() == null ? 0 : info.getHighEarningsDown(),Tag.HIGH_BONUS_DOWN),
                KVVO.of("中收益上升",info.getMediumEarningsUp() == null ? 0 : info.getMediumEarningsUp(),Tag.MID_BONUS_RISE),
                KVVO.of("中收益稳定",info.getMediumEarningsStab() == null ? 0 : info.getMediumEarningsStab(),Tag.MID_BONUS_STABLE),
                KVVO.of("中收益下降",info.getMediumEarningsDown() == null ? 0 : info.getMediumEarningsDown(),Tag.MID_BONUS_DOWN),
                KVVO.of("低收益上升",info.getLowerEarningsUp() == null ? 0 : info.getLowerEarningsUp(),Tag.LOW_BONUS_RISE),
                KVVO.of("低收益稳定",info.getLowerEarningsStab() == null ? 0 : info.getLowerEarningsStab(),Tag.LOW_BONUS_STABLE),
                KVVO.of("低收益下降",info.getLowerEarningsDown() == null ? 0 : info.getLowerEarningsDown(),Tag.LOW_BONUS_DOWN)
        );
        return header;

    }

    /**
     * sec人群分析
     * @param secIdCode
     * @date 2021/5/7 17:44
     * @return java.util.List<com.tengju.bff.interfaces.shared.KVVO>
     */
    public List<KVVO> getSECPeopleAnalysisInfo(Long secIdCode) {
        DashBoardPeopleAnalysisInfo info = dashBoardMarketRespository.getSECPeopleAnalysis(secIdCode);
        List<KVVO> header = List.of(
                KVVO.of("新人低潜力",info.getNewLowerCapacity() == null ? 0 : info.getNewLowerCapacity(), Tag.NEW_USER_LOW_POTENTIAL),
                KVVO.of("新人中潜力",info.getNewMediumCapacity() == null ? 0 : info.getNewMediumCapacity(),Tag.NEW_USER_MID_POTENTIAL),
                KVVO.of("新人高潜力",info.getNewHigherCapacity() == null ? 0 : info.getNewHigherCapacity(),Tag.NEW_USER_HIGH_POTENTIAL),
                KVVO.of("高收益上升",info.getHighEarningsUp() == null ? 0 : info.getHighEarningsUp(),Tag.HIGH_BONUS_RISE),
                KVVO.of("高收益稳定",info.getHighEarningsStab() == null ? 0 : info.getHighEarningsStab(),Tag.HIGH_BONUS_STABLE),
                KVVO.of("高收益下降",info.getHighEarningsDown() == null ? 0 : info.getHighEarningsDown(),Tag.HIGH_BONUS_DOWN),
                KVVO.of("中收益上升",info.getMediumEarningsUp() == null ? 0 : info.getMediumEarningsUp(),Tag.MID_BONUS_RISE),
                KVVO.of("中收益稳定",info.getMediumEarningsStab() == null ? 0 : info.getMediumEarningsStab(),Tag.MID_BONUS_STABLE),
                KVVO.of("中收益下降",info.getMediumEarningsDown() == null ? 0 : info.getMediumEarningsDown(),Tag.MID_BONUS_DOWN),
                KVVO.of("低收益上升",info.getLowerEarningsUp() == null ? 0 : info.getLowerEarningsUp(),Tag.LOW_BONUS_RISE),
                KVVO.of("低收益稳定",info.getLowerEarningsStab() == null ? 0 : info.getLowerEarningsStab(),Tag.LOW_BONUS_STABLE),
                KVVO.of("低收益下降",info.getLowerEarningsDown() == null ? 0 : info.getLowerEarningsDown(),Tag.LOW_BONUS_DOWN)
        );
        return header;

    }

}
